<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Model_Friend extends CI_Model {

	public function __construct() {
		parent::__construct ();
		require(APPPATH.'config/database.php');
		$this->load->database ( ACTIVE_COMPETITION );
	}
	
	public function search_user_by_username( $username_ ) {
		$sql = "SELECT user.userid, user.username, user.plist, user.update_time
				FROM user 
				WHERE user.username LIKE '%.".$this->db->escape_like_str($username_)."%' ";
		log_message('debug', 'search_user_by_username '.$sql);
		$query = $this->db->query( $sql );
		return $query;
	}
	public function update_user_dress( $userid_, $username_, $dress_plist_, $md5sum_, $home_index_, $level_, $age_, $star_, $weight_, $height_, $time_ ) {
		$sql = "SELECT userid, username 
				FROM user 
				WHERE userid = $userid_ ";
		$query = $this->db->query( $sql );
		if ($query->num_rows() > 0) {
			$username_ = $query->row(0)->username;
			$sql = "UPDATE user
					SET plist = '$dress_plist_', md5sum = '$md5sum_', home = $home_index_, level = $level_, age = $age_, star = $star_, weight = $weight_, height = $height_, update_time = '$time_'
					WHERE userid = $userid_ ";
			log_message('debug', 'update_user_dress '.$sql);
			$this->db->query( $sql );
			if ($this->db->affected_rows() > 0) {
				return true;
			}
			else {
				log_message('debug', 'update_user_dress failed '.$sql);
				return false;
			}
		}
		else {
			if ($username_ == null) {
				$user_db = $this->load->database(ACTIVE_ACCOUNT, true);
				$sql = "SELECT username from user_login WHERE userid = $userid_";
				$query = $user_db->query($sql);
				if ($query->num_rows() >= 1) {
					$username_ = $query->row(0)->username;
				}
			}
			
			$sql = "INSERT INTO user(userid, username, plist, md5sum, home, level, age, star, weight, height, update_time)
					VALUES( $userid_, ".$this->db->escape($username_)." , '$dress_plist_', '$md5sum_', $home_index_, $level_, $age_, $star_, $weight_, $height_, '$time_')";
			//log_message('debug', 'add_user_dress '.$sql);
			$this->db->query( $sql );
			if ($this->db->affected_rows() > 0) {
				return true;
			}
			else {
				log_message('error', 'add_user_dress failed '.$sql);
				return false;
			}
		}
	}
	public function is_attention( $inviter_, $invitee_ ) {
		$sql = "SELECT inviter, invitee FROM friend WHERE inviter = $inviter_ AND invitee = $invitee_";
		log_message('debug', 'is_attention '.$sql);
		$query = $this->db->query( $sql );
		if ($query->num_rows() >= 1) {
			return true;
		}
		else {
			return false;
		}
	}
	public function add_attention( $inviter_, $invitee_, $time_ ) {
		$sql = "INSERT INTO friend( inviter, invitee, invite_time) VALUES ( $inviter_, $invitee_, '$time_' )";
		log_message('debug', 'add_attention '.$sql);
		$query = $this->db->query( $sql );
		if ($this->db->affected_rows() > 0) {
			return true;
		}
		else {
			log_message('error', 'add_attention failed '.$sql);
			return false;
		}
	}
	public function remove_attention( $inviter_, $invitee_ ) {
		$sql = "DELETE FROM friend WHERE inviter = $inviter_ AND invitee = $invitee_";
		log_message('debug', 'remove_attention '.$sql);
		$query = $this->db->query( $sql );
		if ($this->db->affected_rows() > 0) {
			return true;
		}
		else {
			log_message('error', 'remove_attention failed '.$sql);
			return false;
		}
	}
	public function get_friend_state( $inviter_, $invitee_ ) {
		$sql = "SELECT state FROM friend WHERE inviter = $inviter_ AND invitee = $invitee_";
		$query = $this->db->query( $sql );
		if ($query->num_rows() >= 1) {
			return $query->row(0)->state;
		}
		else {
			log_message('error', 'get_friend_state have no friend relation '.$sql);
			return null;
		}
	}
	public function set_friend_state( $inviter_, $invitee_, $flag_ ) {
		$sql = "UPDATE friend SET state = $flag_ WHERE inviter = $inviter_ AND invitee = $invitee_";
		log_message('debug', 'set_friend_state '.$sql);
		$query = $this->db->query( $sql );
		if ($this->db->affected_rows() > 0) {
			return true;
		}
		else {
			log_message('error', 'set_friend_state failed '.$sql);
			return false;
		}
	}
	public function update_introduction( $userid_, $msg_, $time_ ) {
		$sql = "UPDATE user SET user.introduction = ".$this->db->escape($msg_).", update_time = '$time_' WHERE user.userid = $userid_ ";
		log_message('debug', 'update_introduction '.$sql);
		$query = $this->db->query( $sql );
		if ($this->db->affected_rows() > 0) {
			return true;
		}
		else {
			log_message('error', 'update_introduction failed '.$sql);
			return false;
		}
	}
	public function add_message( $senderid_, $receiverid_, $msg_, $time_ ) {
		$sql = "INSERT INTO message (senderid, receiverid, msg, send_time) VALUES ($senderid_, $receiverid_, ".$this->db->escape($msg_).", '$time_')";
		log_message('debug', 'add_message '.$sql);
		$query = $this->db->query( $sql );
		if ($this->db->affected_rows() > 0) {
			return true;
		}
		else {
			log_message('error', 'add_message '.$sql);
			return false;
		}
	}
	public function remove_message( $messageid_ ) {
		$sql = "DELETE FROM message WHERE messageid = $messageid_ ";
		log_message('debug', 'remove_message '.$sql);
		$query = $this->db->query( $sql );
		if ($this->db->affected_rows() > 0) {
			return true;
		}
		else {
			log_message('error', 'remove_message '.$sql);
			return false;
		}
	}
	public function get_message_list( $receiverid_, $time_ ) {
		$sql = "SELECT user.userid as senderid, a.receiverid, user.username as sender_name, a.messageid, a.msg, a.send_time
				FROM user INNER JOIN ( SELECT messageid, senderid, receiverid, msg, send_time 
									FROM message
									WHERE message.receiverid = $receiverid_ AND send_time >= '$time_'
									AND NOT EXISTS ( SELECT friend.inviter
																FROM friend
																WHERE friend.inviter = message.senderid AND friend.invitee = message.receiverid AND friend.state = 1) 
									) as a
				ON user.userid = a.senderid
				ORDER BY send_time ";
		
		log_message('debug', 'get_message_list '.$sql);
		return $this->db->query( $sql );
	}
	public function get_attention_list( $inviter_, $time_ ) {
		$sql = "SELECT user.userid, user.username, user.introduction, user.plist, user.md5sum,
				user.home, user.update_time, user.level, user.age, user.star, user.weight, user.height, a.state,
				user.cover_gril_count, user.cover_gril_best_score, user.cover_gril_best_ranking, user.cover_gril_gold, user.cover_gril_silver, user.cover_gril_bronze,
				user.pk_count, user.pk_best_score, user.pk_best_ranking, user.pk_gold, user.pk_silver, user.pk_bronze
				FROM user INNER JOIN ( SELECT friend.inviter, friend.invitee, friend.state FROM friend
									WHERE friend.inviter = $inviter_ AND invite_time > '$time_' ) as a ON user.userid = a.invitee
				WHERE user.update_time > '$time_'";
		
		log_message('debug', 'get_attention_list '.$sql);
		return $this->db->query( $sql );
	}
	public function get_attention( $inviter_, $invitee_ ) {
		$sql = "SELECT user.userid, user.username, user.introduction, user.plist, user.md5sum, 
				user.home, user.update_time, user.level, user.age, user.star, user.weight, user.height, a.state,
				user.cover_gril_count, user.cover_gril_best_score, user.cover_gril_best_ranking, user.cover_gril_gold, user.cover_gril_silver, user.cover_gril_bronze,
				user.pk_count, user.pk_best_score, user.pk_best_ranking, user.pk_gold, user.pk_silver, user.pk_bronze
				FROM user INNER JOIN ( SELECT friend.inviter, friend.invitee, friend.state FROM friend 
									WHERE friend.inviter = $inviter_ AND friend.invitee = $invitee_ ) as a ON user.userid = a.invitee";
		log_message('debug', 'get_attention '.$sql);
		return $this->db->query( $sql );
	}
	public function get_fans_list( $invitee_, $time_ ) {
				
		$sql = "SELECT user.userid, user.username, user.introduction, user.plist, user.md5sum,
				user.home, user.update_time, user.level, user.age, user.star, user.weight, user.height, a.state,
				user.cover_gril_count, user.cover_gril_best_score, user.cover_gril_best_ranking, user.cover_gril_gold, user.cover_gril_silver, user.cover_gril_bronze,
				user.pk_count, user.pk_best_score, user.pk_best_ranking, user.pk_gold, user.pk_silver, user.pk_bronze
				FROM user INNER JOIN ( SELECT friend.inviter, friend.invitee, friend.state FROM friend
									WHERE friend.invitee = $invitee_ AND invite_time > '$time_' ) as a ON user.userid = a.invitee
				WHERE user.update_time > '$time_'";
		
		log_message('debug', 'get_fans_list '.$sql);
		return $this->db->query( $sql );
	}
	
	public function is_message_update( $userid_, $time_ ) {
		$sql = "SELECT messageid FROM message WHERE receiverid = $userid_ AND send_time > $time_";
		$query = $this->db->query($sql);
		if ($query->num_rows() >= 1) {
			return true;
		}
		else {
			return false;
		}
	}
}
